
package com.android.formula.activity;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.formula.R;
import com.android.formula.adapter.TBaseAdapter;
import com.google.android.gms.ads.AdListener;

public abstract class BaseGridViewActivity<T> extends BaseActivity {

    protected static final int SHOW_NO_DATA_MSG = 0;

    protected static final int SHOW_DATA_MSG = 1;

    protected GridView mGridView;

    protected TextView mEmptyView;

    /** The data of list. */
    protected ArrayList<T> mListItemDatas;

    /** The behind data of list. */
    protected TBaseAdapter<T> mAdapter;

    /**
     * The static value which set to {@link mLoadState} when loading data for
     * list view..
     */
    protected final int IS_LOADING = 0;

    /**
     * The static value which set to {@link mLoadState} when finish loading data
     * for list view..
     */
    protected final int IS_FINISHED_LOADING = 1;

    private int mMsgNoDataId = R.string.no_result_msg;

    /** The state of List view: loading, finished loading.... */
    protected AtomicInteger mLoadState = new AtomicInteger(IS_LOADING);

    protected Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SHOW_DATA_MSG:
                    mLoadState.set(mLoadState.get() & ~IS_LOADING);
                    mLoadState.set(mLoadState.get() | IS_FINISHED_LOADING);
                    mAdapter = getAdapter();
                    mGridView.setAdapter(mAdapter);
                    mGridView.setVisibility(View.VISIBLE);
                    break;
                case SHOW_NO_DATA_MSG:
                    mEmptyView.setVisibility(View.VISIBLE);
                    mEmptyView.setText(mMsgNoDataId);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void initView() {
        setContentView(getLayout());
        mGridView = getGridView();
        mEmptyView = getEmptyView();
        mGridView.setEmptyView(mEmptyView);
        mGridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                T clickedItem = mListItemDatas.get(pos);
                clickOnPos(clickedItem, pos);
            }
        });
        setAdListener();
    }

    @Override
    public void loadDataScreen() {
        // Change state to loading.
        mLoadState.set(mLoadState.get() | IS_LOADING);

        if (mListItemDatas == null) {
            mListItemDatas = new ArrayList<T>();
        } else {
            mListItemDatas.clear();
        }

        getData();
    }

    /**
     * Get grid view from layout.
     */
    protected GridView getGridView() {
        return (GridView) findViewById(R.id.gridview);
    }

    protected TextView getEmptyView() {
        return (TextView) findViewById(R.id.txt_no_result);
    }

    /**
     * Return the id of layout of fragment.
     */
    protected int getLayout() {
        return R.layout.activity_gridview;
    }

    /**
     * Execute when click on specified position of list view.
     */
    protected abstract void clickOnPos(T clickedItem, int pos);

    /**
     * Get data for list view.
     */
    protected void getData() {
        mEmptyView.setVisibility(View.GONE);
    }

    protected void showData() {
        mHandler.sendEmptyMessageDelayed(SHOW_DATA_MSG, 100);
    }

    /**
     * Execute when there is no data.
     */
    protected void showNoData() {
        mHandler.sendEmptyMessageDelayed(SHOW_NO_DATA_MSG, 100);
    }

    /**
     * Get the adapter.
     */
    protected abstract TBaseAdapter<T> getAdapter();

    protected void setMsgNoData(int msgId) {
        mMsgNoDataId = msgId;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void setAdListener() {
        final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        mAdListener = new AdListener() {
            @Override
            public void onAdFailedToLoad(int errorCode) {
                super.onAdFailedToLoad(errorCode);
                mAdView.setVisibility(View.GONE);
            }

            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                mAdView.setVisibility(View.VISIBLE);
                params.addRule(RelativeLayout.ABOVE, R.id.gridview_layout_ads);
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
                mGridView.setLayoutParams(params);
            }
        };
    }
}
